//
//  ZPwaihuDetailViewController.m
//  HCNewGoldFinger
//
//  Created by 刘琛 on 2017/6/19.
//  Copyright © 2017年 zjhcsoftios. All rights reserved.
//

#import "ZPwaihuDetailViewController.h"
#import "ZPwaihuDeatailCell.h"
#import "NetworkAddress.h"

@interface ZPwaihuDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ZPwaihuDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"外呼详情";
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - Height_NavBar) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZPwaihuDeatailCell class] forCellReuseIdentifier:@"ZPwaihuDeatailCellIdentify"];
        [self.view addSubview:_tableView];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)setModel:(ZPchaxunModel *)model {
    _model = model;
    [self.tableView reloadData];
}

#pragma tableDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _tableView) {
        
        ZPchaxunDetailModel *temp = _model.dataList[indexPath.row];
        int height = 26;
        CGSize size1 = [self calculateLabelSizeOfContent:temp.text withFont:[UIFont systemFontOfSize:14] maxSize:CGSizeMake(100, 9999)];
        CGSize size2 = [self calculateLabelSizeOfContent:temp.value withFont:[UIFont systemFontOfSize:13] maxSize:CGSizeMake(170, 9999)];
        if (size1.height>size2.height) {
            height=height+size1.height;
        }else{
            height=height+size2.height;
        }
        return height;
    }else{

        
        return 44;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _model.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZPwaihuDeatailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZPwaihuDeatailCellIdentify" forIndexPath:indexPath];
    cell.model = _model.dataList[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (CGSize)calculateLabelSizeOfContent:(NSString*)text withFont:(UIFont*)font maxSize:(CGSize)aMaxSize
{
    
    const CGSize defaultSize = CGSizeMake(kScreenWidth, 22);
    
    if (text == nil || text.length == 0) {
        return defaultSize;
    }
    
    CGSize labelSize = aMaxSize;
    if ([text isKindOfClass:[NSString class]]) {
        //       [text sizeWithFont:
        
        CGRect stringRect;
        
        labelSize = [text sizeWithFont:font constrainedToSize:aMaxSize lineBreakMode:NSLineBreakByWordWrapping];
        
        if ([text respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
            stringRect = [text boundingRectWithSize:aMaxSize
                                            options:NSStringDrawingUsesLineFragmentOrigin
                                         attributes:@{NSFontAttributeName:font}
                                            context:NULL];
        } else {
            CGSize stringSize;
            
            stringSize = [text sizeWithFont:font constrainedToSize:aMaxSize lineBreakMode:NSLineBreakByWordWrapping];
            
            stringRect = CGRectMake(0.0f, 0.0f, stringSize.width, stringSize.height);
        }
        labelSize = stringRect.size;
    }
    return labelSize;
}

@end
